(function() {
  var express, fs, iced, mkdirp, moment, path, pgsql, router, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  moment = require('moment');

  fs = require('fs');

  mkdirp = require('mkdirp');

  path = require('path');

  router = express.Router();

  module.exports = router;

  router.all('/', function(req, res, next) {
    var client, done, err, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.stat_title = '消费代付汇总';
    res.locals.start_date = req.body.start_date || req.query.start_date || moment().format('YYYY-MM-DD');
    res.locals.end_date = req.body.end_date || req.query.end_date || moment().format('YYYY-MM-DD');
    where_clause = "o.sys_time::date >= '" + res.locals.start_date + "'\nand o.sys_time::date <= '" + res.locals.end_date + "'";
    res.locals.total = 1;
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/p1_order_sum.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 29
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/p1_order_sum.coffee"
          });
          client.query("select count(o.*) as count, sum(o.p1_fee) as fee, case when t.chnl in (1,4) then sum(cast(json_extract_path_text(i_pkt,'4') as decimal)) / 100 end as amt from pay_p1_order as o left join pay_trlist as t on t.uuid = o.t20_uuid and t.uuid_no = 1 where " + where_clause + " group by t.chnl", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 43
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.t20_stats = result.rows[0] || {};
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/p1_order_sum.coffee"
            });
            client.query("select count(o.*) as count, case when t.chnl in (1,4) then sum(cast(json_extract_path_text(i_pkt,'4') as decimal)) / 100 end as amt from pay_p1_order as o left join pay_trlist as t on t.uuid = o.t20_uuid and t.uuid_no = 1 where " + where_clause + " and o.status = 1 group by t.chnl", __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 57
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            res.locals.t40_status_1 = result.rows[0] || {};
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/p1_order_sum.coffee"
              });
              client.query("select count(o.*) as count, case when t.chnl = 3 then sum(cast(json_extract_path_text(i_pkt,'txnAmt') as decimal)) / 100 end as amt from pay_p1_order as o left join pay_trlist as t on t.uuid = o.t40_uuid and t.incoming = false and json_extract_path_text(t.trinfo, 'code')::text = 't40' where " + where_clause + " and o.status = 2 group by t.chnl", __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 72
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              res.locals.t40_status_2 = result.rows[0] || {};
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/p1_order_sum.coffee"
                });
                client.query("select count(o.*) as count, case when t.chnl = 3 then sum(cast(json_extract_path_text(i_pkt,'txnAmt') as decimal)) / 100 end as amt from pay_p1_order as o left join pay_trlist as t on t.uuid = o.t40_uuid and t.incoming = false and json_extract_path_text(t.trinfo, 'code')::text = 't40' where " + where_clause + " and o.status = 3 group by t.chnl", __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 87
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  done(client);
                  return next(err);
                }
                res.locals.t40_status_3 = result.rows[0] || {};
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/trstat/p1_order_sum.coffee"
                  });
                  client.query("select count(o.*) as count, case when t.chnl = 3 then sum(cast(json_extract_path_text(i_pkt,'txnAmt') as decimal)) / 100 end as amt from pay_p1_order as o left join pay_trlist as t on t.uuid = o.t40_uuid and t.incoming = false and json_extract_path_text(t.trinfo, 'code')::text = 't40' where " + where_clause + " and o.status = 4 group by t.chnl", __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        err = arguments[0];
                        return result = arguments[1];
                      };
                    })(),
                    lineno: 102
                  }));
                  __iced_deferrals._fulfill();
                })(function() {
                  if (err) {
                    done(client);
                    return next(err);
                  }
                  res.locals.t40_status_4 = result.rows[0] || {};
                  done();
                  return res.render('trstat/p1_order_sum');
                });
              });
            });
          });
        });
      };
    })(this));
  });

}).call(this);
